package handler.server;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import util.NettyUtil;

import java.util.Date;

public class FirstServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ByteBuf byteBuf = (ByteBuf) msg;
        // 服务端读取数据
        System.out.println(new Date() + " : 服务端读到数据 -> " + NettyUtil.getStringByteBuff(byteBuf));

        // 服务端往客户端写数据
        System.out.println("服务端写数据！");
        String text = "你好，欢迎关注我的微信公众号，《闪电侠的博客》!";
        ctx.channel().writeAndFlush(NettyUtil.getByteBuff(ctx, text));
    }
}